import re
import time
import sys
import bs4
from pyvirtualdisplay import Display

from kafka import KafkaProducer
from selenium import webdriver


# 返回无页面自动化测试浏览器
def get_NOHEAD_browser():
    # chrome_options = Options()
    # chrome_options.add_argument('--headless')
    # #谷歌浏览器应用
    # path = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
    # chrome_options.binary_location = path

    # 注意：使用handless的时候，需要同时将驱动文件路径加入到CHrome对象的创建中（因为我没有在电脑中添加环境变量）
    browser = webdriver.Firefox()
    browser.set_window_size(0, 0)
    return browser


def get_content():
    browser = get_NOHEAD_browser()

    browser.minimize_window()

    browser.get('http://www.weather.com.cn/weather1d/101250101.shtml')

    time.sleep(5)

    content = browser.page_source

    browser.close()

    return content


def analyze_content(content):
    soup = bs4.BeautifulSoup(content, 'html.parser')

    # 实时时间
    t = soup.select('#today > div.t > div > p.time > span')[0].get_text().split(' ')[0]
    # 相对湿度
    humidity = soup.select('#today > div.t > div > div.zs.h > em')[0].get_text()
    # 风向
    wind_direction = soup.select('#today > div.t > div > div.zs.w > span')[0].get_text()
    # 风级
    wind_level = soup.select('#today > div.t > div > div.zs.w > em')[0].get_text()
    # 温度
    temp = soup.select('#today > div.t > div > div.tem > span')[0].get_text() + '℃'
    # 实时空气质量指数（AQI）
    air_condition = soup.select('#today > div.t > div > div.zs.pol > span > a')[0].get_text()

    # 天气情况  只能通过input隐藏输入框中的属性得到天气情况，其他这个网站都没有
    weather_init = soup.select('#hidden_title')[0]
    weather = weather_init.attrs.get('value').split(" ")[3]

    wind_level = re.split('(\d+)', wind_level)[1]
    humidity = re.split('(\d+)', humidity)[1]
    temp = re.split('(\d+)', temp)[1]
    air_condition_number = re.split('(\d+)', air_condition)[1]
    air_condition_level = re.split('(\d+)', air_condition)[2]

    # df = pd.DataFrame({'weather': [weather],
    #                    'time': [time],
    #                    'humidity': [humidity],
    #                    'wind': [wind],
    #                    'temperature': [temp],
    #                    'airCondition': [air_condition]})

    df = weather + \
         ',' + t + \
         ',' + humidity + \
         ',' + wind_direction + \
         ',' + wind_level + \
         ',' + temp + \
         ',' + air_condition_number + \
         ',' + air_condition_level + \
         ',' + str(time.strftime('%Y%m%d%H%M%S', time.localtime()))

    return df


if __name__ == '__main__':
    display = Display(visible=False, size=(1920, 1080))
    display.start()

    bootstrapServers = str(sys.argv[1])
    topic = str(sys.argv[2])
    message = str(sys.argv[3])
    producer = KafkaProducer(bootstrap_servers=bootstrapServers)

    if message != 'CLOSE':
        try:
            content = get_content()
            value = analyze_content(content)
            producer.send(topic=topic, value=value.encode())
            # producer = KafkaProducer(bootstrap_servers="master:9092")
            # producer.send(topic="weather", value=value.encode())
        except:
            value = "ERROR:timekey=" + str(time.strftime('%Y%m%d%H%M%S', time.localtime()))
            producer.send(topic=topic, value=value.encode())
    else:
        producer.send(topic=topic, value=message.encode())
    time.sleep(5)
    producer.close()
    display.stop()


# while True:
#     # content = get_content()
#     # value = analyze_content(content)
#     # print(value)
#     startTime = time.time()
#     value = ""
#     try:
#         content = get_content()
#         value = analyze_content(content)
#     except:
#         value = "," + str(datetime.now())[0:19]
#     finally:
#         print(value)
#         sleepTime = 150.0 - time.time() + startTime
#         if sleepTime > 0.0:
#             time.sleep(sleepTime)

# 存在文件就删掉再更新
# if os.path.exists('weather_of_ChangSha.csv'):
#     os.remove('weather_of_ChangSha.csv')

# 不要表头，不要索引
# df.to_csv('weather_of_ChangSha.csv', header=None, index=None)
